package com.wecloud.core.action.bussiness;

import java.util.HashMap;
import java.util.Map;

import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.wecloud.core.action.impl.BaseTransCfmAction;
import com.wecloud.core.constant.ErrorMsg;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;
import com.wecloud.core.utils.RegexValidateUtil;

/**
 * 注册服务
 * @author qq374
 *
 */
public class RegisterAction extends BaseTransCfmAction{
	public static final String ROLE = "0";//普通用户
	@Autowired
	private JnlService jnlService;//mysql
	@Override
	public void excute(WeContext context)  throws WeException{
		String userName = context.getString("userName");
		String passWord = context.getString("passWord");
		String email = context.getString("email");
		String telphone = context.getString("telphone");
		String sex = context.getString("sex");
		
		//检查用户名是否为空
		if(CommonUtils.isEmpty(userName)){
			throw new WeException(ErrorMsg.Error_0004);
		}
		if(CommonUtils.isEmpty(passWord)){
			throw new WeException(ErrorMsg.Error_0005);
		}
		if(CommonUtils.isNotEmpty(email) && !RegexValidateUtil.checkEmail(email)){
			throw new WeException(ErrorMsg.Error_0007);
		}
		if(CommonUtils.isNotEmpty(telphone) && !RegexValidateUtil.checkEmail(telphone)){
			throw new WeException(ErrorMsg.Error_0008);
		}
		if(CommonUtils.isEmpty(sex)){
			throw new WeException(ErrorMsg.Error_0009);
		}
		//检查用户名是否存在
		Map map = new HashMap();
		map.put("userName", userName);
		Map resultMap = jnlService.queryForObject("user.queryUserByName", map);
		if(resultMap != null){
			throw new WeException(ErrorMsg.Error_0006);
		}
		//检查手机号码是否已注册
		map.put("telphone", telphone);
		Map resultMap1 = jnlService.queryForObject("user.queryUserByPhone", map);
		if(resultMap1 != null){
			throw new WeException(ErrorMsg.Error_0010);
		}
		//检查邮箱是否已注册
		map.put("email", email);
		Map resultMap2 = jnlService.queryForObject("user.queryUserByEmail", map);
		if(resultMap2 != null){
			throw new WeException(ErrorMsg.Error_0011);
		}
	}
}
